package eu.sudparis.it.opp;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Node {
	public List<Node> inContact = new ArrayList<Node>();
	public int id;
	public Node chain_of_responsibility;

	public final void wrap(Node nd, Graph g) {
		this.chain_of_responsibility = nd;
		id = nd.id;
		nd.inContact = this.inContact;
		g.graph.put(id, this);
	}

	public final void embedInto(Node nd, Graph g) {
		nd.chain_of_responsibility = this;
		this.inContact = nd.inContact;
		this.id = nd.id;
		g.graph.put(id, nd);
	}

	public void encounter(Node nd) {
		if (!this.inContact.contains(nd)) {
			this.inContact.add(nd);
			nd.inContact.add(this);
		}
	//	if(this.chain_of_responsibility!=null)
	//		this.chain_of_responsibility.encounter(nd);
	}

	public void leaveWith(Node nd) {
		this.inContact.remove(nd);
		nd.inContact.remove(this);
	//	if(this.chain_of_responsibility!=null)
	//		this.chain_of_responsibility.leaveWith(nd);
	}

	public void communicate(Set<Node> nods) {

	}

	public Node clone() {
		return new Node();
	}

	public List<Node> getMANET() {
		List<Node> nods = new ArrayList<Node>();
		this.travel(nods);
		return nods;
	}

	private void travel(List<Node> traveled) {
		if (traveled.contains(this)) {
			return;
		} else {
			traveled.add(this);
			for (Node nd : this.inContact) {
				nd.travel(traveled);
			}
		}
	}

	public boolean equals(Object obj) {
		return this.id == ((Node) obj).id;
	}

	public String toString() {
		return "N" + this.id;
	}
}
